package com.eateateat.controller;



import com.eateateat.entity.Menu2;
import com.eateateat.service.AdminService;
import com.eateateat.service.MenuService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController  // @Controller+@ResponseBody
@RequestMapping("/menu")
public class MenuController {
    Logger logger = Logger.getLogger(MenuController.class);
    @Autowired
    MenuService menuService2;
    @Autowired
    AdminService adminService;
//    Dao dao;

    @PostMapping("/insert") //menuMapper.xml
    String insert(HttpServletRequest request, Menu2 menu, @RequestParam Map map, MultipartFile pic) throws IOException {
        HashMap m = (HashMap) request.getSession().getAttribute("islogin");
        logger.debug(m); //{lxid=4, lxtel=null, lxpass=Yi123456, lxaddr=1, usertype=1, lxname=哒玲玲}
        int sellid = (int) m.get("lxid");
        menu.setLxsellid(sellid);
        logger.debug(menu);
        if (menuService2.insertMenu(menu)){
//            fileName = request.getServletContext().getRealPath("/") + "upload/"
//                    + menu.getLxname() + "_.png";//假设增加后的序列号做图片
            logger.debug(menu.getLxid());
            String fileName = menu.getLxid() + "_.png";
            logger.debug(fileName);
            pic.transferTo(new File(fileName));//会直接传到application里的spring.servlet.multipart.location=D:/upload/imgs/
            logger.debug(fileName);
            logger.debug("success");
            return "success";}
        else return "failure";
    }

    @GetMapping("/select")
    List select(@RequestParam Map map,HttpServletRequest req) throws IOException {
        logger.debug(map);
//        Map m= (Map) req.getSession().getAttribute("selllogin");
//        logger.debug(m);
        Map m2= (Map) req.getSession().getAttribute("islogin");
        logger.debug(m2);//{lxmemid=哒玲玲, lxmemalias=1, usertype=2, lxmempass=Yi123456}

        int usertype= (int) m2.get("usertype");
        if(usertype==1||usertype==2){
            map.put("lxsellid",m2.get("lxid"));
            logger.debug(map);
            List lst = menuService2.getMenu(map);//{lxmemid=哒玲玲, lxmemalias=1, usertype=2, lxmempass=Yi123456}
            logger.debug("select success");
            return lst;
        } else if (usertype == 0){
//            map.put("lxsellid",m2.get("lxid"));
            logger.debug(map);
            List lst = adminService.getMenu(map);//{lxmemid=哒玲玲, lxmemalias=1, usertype=2, lxmempass=Yi123456}
            logger.debug("select success");
            return lst;
        }
        return null;
    }

    @GetMapping("/check2")
    String check2(HttpServletRequest req, @RequestParam Map map) throws IOException {
        logger.debug(map);//{lxname=aaa}
//        PrintWriter out = response.getWriter();
//        Map map1= (Map) req.getSession().getAttribute("selllogin");
        Map map1= (Map) req.getSession().getAttribute("islogin");
        logger.debug(map1);// {lxid=4, lxtel=null, lxpass=Yi123456, lxaddr=1, lxname=哒玲玲}
//        map1.get("lxid");//meiyouyiyi
//        map1.putAll(map);
        int lxid = (int) map1.get("lxid");
        map.put("lxid",lxid);
//        map.putAll(map1);
        logger.debug(map);
        if (menuService2.check(map)) {
            return "have";
        } else {
            return "meiyou";
        }
    }

//    @GetMapping("/check")
//    String check(HttpServletRequest req, HttpServletResponse response) throws IOException {
//        String name = req.getParameter("lxwname");
//        logger.debug(name);
////        PrintWriter out = response.getWriter();
//        if (menuService2.checkname(name)) {
////            out.print("have");
//            return ("have");
//        } else {
////            out.print("meiyou");
//            return "meiyou";
//        }
//    }


//    @GetMapping("/delete")
//    void delete(HttpServletResponse response, HttpServletRequest request, @RequestParam Map map) throws IOException {
//        PrintWriter out = response.getWriter();
//        if (menuService2.delete(map)) {
//            out.print("delete success!");
//        } else {
//            out.print("delete failure!");
//        }
//    }
//  @GetMapping("/delete")旧
//    String delete(HttpServletResponse response, HttpServletRequest request, @RequestParam Map map) throws IOException {
//        logger.debug(map);
//        PrintWriter out = response.getWriter();
//        if (menuService2.delete(map)) {
//            return ("success!");
//        } else {
//            return("delete failure!");
//
//        }
//    }/
  @GetMapping("/delete/{lxid}")//spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
    String delete(HttpServletResponse response, HttpServletRequest request, @PathVariable int lxid) throws IOException {
        logger.debug(lxid);
        if (menuService2.delete(lxid)) {
            return ("success!");
        } else {
            return("delete failure!");

        }
    }


}
